package com.ruoyi.question.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.question.domain.QueList;
import com.ruoyi.question.service.IQueListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 清单管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-26
 */
@Controller
@RequestMapping("/questionlist/quelist")
public class QueListController extends BaseController
{
    private String prefix = "questionlist/quelist";

    @Autowired
    private IQueListService queListService;

    @RequiresPermissions("questionlist:quelist:view")
    @GetMapping()
    public String quelist()
    {
        return prefix + "/quelist";
    }

    /**
     * 查询清单管理列表
     */
    @RequiresPermissions("questionlist:quelist:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QueList queList)
    {
        startPage();
        List<QueList> list = queListService.selectQueListList(queList);
        return getDataTable(list);
    }

    /**
     * 导出清单管理列表
     */
    @RequiresPermissions("questionlist:quelist:export")
    @Log(title = "清单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QueList queList)
    {
        List<QueList> list = queListService.selectQueListList(queList);
        ExcelUtil<QueList> util = new ExcelUtil<QueList>(QueList.class);
        return util.exportExcel(list, "清单管理数据");
    }

    /**
     * 新增清单管理
     */
    @RequiresPermissions("questionlist:quelist:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存清单管理
     */
    @RequiresPermissions("questionlist:quelist:add")
    @Log(title = "清单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QueList queList)
    {
        return toAjax(queListService.insertQueList(queList));
    }

    /**
     * 修改清单管理
     */
    @RequiresPermissions("questionlist:quelist:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        QueList queList = queListService.selectQueListById(id);
        mmap.put("queList", queList);
        return prefix + "/edit";
    }

    /**
     * 修改清单细则
     */
    @RequiresPermissions("questionlist:quelist:edit")
    @PostMapping("/detailsIds")
    public AjaxResult detailsIds(@RequestBody JsonNode request)
    {
        // 获取请求参数
        String listId = request.get("listId").asText(); // listId 是字符串
        JsonNode idsNode = request.get("ids"); // ids 是数组
        List<String> ids = new ArrayList<>();
        if (idsNode.isArray()) {
            for (JsonNode idNode : idsNode) {
                ids.add(idNode.asText());
            }
        }
        AjaxResult ajaxResult = toAjax(queListService.addQueListByIds(ids, listId));
        System.out.println(ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改保存清单管理
     */
    @RequiresPermissions("questionlist:quelist:edit")
    @Log(title = "清单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QueList queList)
    {
        return toAjax(queListService.updateQueList(queList));
    }

    /**
     * 删除清单管理
     */
    @RequiresPermissions("questionlist:quelist:remove")
    @Log(title = "清单管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(queListService.deleteQueListByIds(ids));
    }
}
